import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
    followUser,
    unfollowUser,
    getFollowingList,
    getFollowersList
} from '@/api/follow'

export const useFollowsStore = defineStore('follows', () => {
    // 状态
    const followingList = ref([]) // 关注列表
    const followersList = ref([]) // 粉丝列表
    const loading = ref(false)
    const hasMoreFollowing = ref(true)
    const hasMoreFollowers = ref(true)
    const followingPage = ref(1)
    const followersPage = ref(1)
    const pageSize = ref(20)

    // Actions

    // 关注用户
    const followUserAction = async (followId) => {
        try {
            const response = await followUser({ follow_id: followId })
            if (response.code === 0) {
                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        }
    }

    // 取消关注
    const unfollowUserAction = async (followId) => {
        try {
            const response = await unfollowUser(followId)
            if (response.code === 0) {
                // 从关注列表中移除
                followingList.value = followingList.value.filter(item => item.follow_id !== followId)
                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        }
    }

    // 获取关注列表
    const fetchFollowingList = async (userId, params = {}) => {
        try {
            loading.value = true
            const response = await getFollowingList(userId, {
                page: followingPage.value,
                pageSize: pageSize.value,
                ...params
            })

            if (response.code === 0) {
                if (params.page === 1 || followingPage.value === 1) {
                    followingList.value = response.data.list || []
                } else {
                    followingList.value = [...followingList.value, ...(response.data.list || [])]
                }

                // 检查是否还有更多数据
                const currentList = response.data.list || []
                hasMoreFollowing.value = currentList.length === pageSize.value

                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        } finally {
            loading.value = false
        }
    }

    // 加载更多关注
    const loadMoreFollowing = async (userId, params = {}) => {
        if (!hasMoreFollowing.value || loading.value) return

        followingPage.value += 1
        return await fetchFollowingList(userId, params)
    }

    // 获取粉丝列表
    const fetchFollowersList = async (userId, params = {}) => {
        try {
            loading.value = true
            const response = await getFollowersList(userId, {
                page: followersPage.value,
                pageSize: pageSize.value,
                ...params
            })

            if (response.code === 0) {
                if (params.page === 1 || followersPage.value === 1) {
                    followersList.value = response.data.list || []
                } else {
                    followersList.value = [...followersList.value, ...(response.data.list || [])]
                }

                // 检查是否还有更多数据
                const currentList = response.data.list || []
                hasMoreFollowers.value = currentList.length === pageSize.value

                return Promise.resolve(response)
            } else {
                return Promise.reject(new Error(response.msg))
            }
        } catch (error) {
            return Promise.reject(error)
        } finally {
            loading.value = false
        }
    }

    // 加载更多粉丝
    const loadMoreFollowers = async (userId, params = {}) => {
        if (!hasMoreFollowers.value || loading.value) return

        followersPage.value += 1
        return await fetchFollowersList(userId, params)
    }

    // 重置关注列表分页
    const resetFollowingPagination = () => {
        followingPage.value = 1
        hasMoreFollowing.value = true
        followingList.value = []
    }

    // 重置粉丝列表分页
    const resetFollowersPagination = () => {
        followersPage.value = 1
        hasMoreFollowers.value = true
        followersList.value = []
    }

    // 重置所有状态
    const resetAll = () => {
        followingList.value = []
        followersList.value = []
        loading.value = false
        hasMoreFollowing.value = true
        hasMoreFollowers.value = true
        followingPage.value = 1
        followersPage.value = 1
    }

    // 从关注列表中移除用户
    const removeFromFollowing = (followId) => {
        followingList.value = followingList.value.filter(item => item.follow_id !== followId)
    }

    // 从粉丝列表中移除用户
    const removeFromFollowers = (userId) => {
        followersList.value = followersList.value.filter(item => item.user_id !== userId)
    }

    return {
        // 状态
        followingList,
        followersList,
        loading,
        hasMoreFollowing,
        hasMoreFollowers,
        followingPage,
        followersPage,
        pageSize,

        // Actions
        followUserAction,
        unfollowUserAction,
        fetchFollowingList,
        loadMoreFollowing,
        fetchFollowersList,
        loadMoreFollowers,
        resetFollowingPagination,
        resetFollowersPagination,
        resetAll,
        removeFromFollowing,
        removeFromFollowers
    }
})